<template>
  <div class="box">
    <div class="bgc">
      <el-row style="height:40px">
        <el-col :span="20">传智教育</el-col>
        <el-col :span="4">
          <el-row>
            <el-col :span="12">负责人</el-col>
            <el-col :span="12">
              <el-dropdown>
                <span> 操作<i class="el-icon-arrow-down el-icon--right" /></span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item @click.native="addDivision('')">添加子部门</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
      <el-tree :data="data" :props="defaultProps">
        <template slot-scope="{ data }">
          <el-row style="width:100%; ">
            <el-col :span="20">{{ data.name || '' }}</el-col>
            <el-col :span="4">
              <el-row>
                <el-col :span="12">负责人</el-col>
                <el-col :span="12">
                  <el-dropdown>
                    <span> 操作<i class="el-icon-arrow-down el-icon--right" /></span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="addDivision(data.id)">添加子部门</el-dropdown-item>
                      <el-dropdown-item @click.native="updateDivision(data)">编辑部门</el-dropdown-item>
                      <el-dropdown-item @click.native="delDivision(data.id)">删除部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- 对话框 -->
    <el-dialog :title="isEditor ? '编辑' : '添加'" :visible.sync="dialogTableVisible">
      <AddDialog
        v-if="dialogTableVisible"
        :dialog-table-visible.sync="dialogTableVisible"
        :pid="pid"
        :is-editor="isEditor"
        :row="row"
        @success="getDepartmentInfo"
      />
    </el-dialog>
  </div>
</template>

<script>
import { delDepartmentsInfo } from '@/api/departments'
import { mapState } from 'vuex'

import AddDialog from './addDialog.vue'

export default {

  components: {
    AddDialog
  },

  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      sumTypeCkeck: ['操作', '计算'],
      dialogTableVisible: false,
      pid: '',
      isEditor: false,
      row: {}
    }
  },

  computed: {
    ...mapState('departments', ['data'])
  },

  created() {
    this.$store.dispatch('departments/getDepartmentInfo')
  },
  methods: {
    //  添加子部门
    addDivision(pid) {
      this.isEditor = false
      this.dialogTableVisible = true
      this.pid = pid
    },

    // 删除部门
    async delDivision(id) {
      const res = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      if (res === 'confirm') {
        const resolve = await delDepartmentsInfo(id)
        if (resolve.code === 10000) {
          this.$message.success('删除成功！')
          this.getDepartmentInfo()
        }
      }
    },

    // 更新部门
    updateDivision(row) {
      this.isEditor = true
      this.dialogTableVisible = true
      this.row = row
    }
  }
}
</script>
<style lang='scss'  scoped>
.box {
  padding: 50px;
  .bgc {
    padding: 20px 100px;
  }
  .el-col {
    border: 1px solid transparent;
  }
}
</style>
